package cloud.maque.module.system.extend.impl;

import cloud.maque.module.system.entity.*;
import cloud.maque.module.system.extend.UserExtendSerivce;
import cloud.maque.module.system.model.UserDto;
import cloud.maque.module.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserExtendSerivceImpl implements UserExtendSerivce {


    @Autowired
    UserService userService;
    @Autowired
    UserExtService userExtService;
    @Autowired
    UserDeptService userDeptService;

    @Autowired
    UserPostService userPostService;

    @Autowired
    UserRoleService userRoleService;

    @Override
    public UserDto GetUserFullInfo(String userId) {

        UserDto dto = new UserDto();
        SysUser user = userService.getById(userId);
        if (user == null) {
            return null;
        }

        dto.setUser(user);
        dto.setUserExt(userExtService.getById(userId));

        SysUserDept dept = new SysUserDept();
        dept.setUserId(userId);
        dto.setUserDepts(userDeptService.GetList(dept));

        SysUserPost post = new SysUserPost();
        post.setUserId(userId);
        dto.setUserPosts(userPostService.GetList(post));

        SysUserRole role = new SysUserRole();
        role.setUserId(userId);
        dto.setUserRoles(userRoleService.GetList(role));

        return dto;
    }

    @Override
    @Transactional
    public UserDto RegUser(UserDto user) {

        UserDto dto = new UserDto();

        SysUser sysUser = userService.NewUser(user.getUser());
        dto.setUser(sysUser);

        if (user.getUserExt() != null) {
            user.getUserExt().setUserId(sysUser.getUserId());
        } else {
            user.setUserExt(new SysUserExt());
        }

        userExtService.SaveUserExt(user.getUserExt());

        if (user.getUserDepts() != null) {
            user.getUserDepts().forEach(x -> {
                x.setUserId(sysUser.getUserId());
            });
            userDeptService.SaveBatch(user.getUserDepts());
        }

        if (user.getUserPosts() != null) {
            user.getUserPosts().forEach(x -> {
                x.setUserId(sysUser.getUserId());
            });
            userPostService.saveBatch(user.getUserPosts());
        }

        if (user.getUserRoles() != null) {
            user.getUserRoles().forEach(x -> {
                x.setUserId(sysUser.getUserId());
            });
            userRoleService.saveBatch(user.getUserRoles());
        }


        return dto;
    }

    @Override
    public UserDto EditUser(UserDto user) {
        UserDto dto = new UserDto();

        SysUser sysUser = userService.EditUser(user.getUser());
        dto.setUser(sysUser);

        //扩展表
        if (user.getUserExt() != null) {
            user.getUserExt().setUserId(sysUser.getUserId());
        } else {
            user.setUserExt(new SysUserExt());
        }
        userExtService.SaveUserExt(user.getUserExt());


        //部门
        userDeptService.DeleteByUserId(sysUser.getUserId());
        if (user.getUserDepts() != null) {
            user.getUserDepts().forEach(x -> {
                x.setUserId(sysUser.getUserId());
            });
            userDeptService.SaveBatch(user.getUserDepts());
        }

        //岗位
        userPostService.DeleteByUserId(sysUser.getUserId());
        if (user.getUserPosts() != null) {
            user.getUserPosts().forEach(x -> {
                x.setUserId(sysUser.getUserId());
            });
            userPostService.saveBatch(user.getUserPosts());
        }

        //角色
        userRoleService.DeleteByUserId(sysUser.getUserId());
        if (user.getUserRoles() != null) {
            user.getUserRoles().forEach(x -> {
                x.setUserId(sysUser.getUserId());
            });
            userRoleService.saveBatch(user.getUserRoles());
        }

        return dto;
    }
}
